import Vector2 from "../../src/maths/Vector2";
import TestApplication from "./TestApplication";
import Math2D from "../../src/maths/Math2D";

export default class Main {
    public constructor() {
        /* const vec: Vector2 = new Vector2(2, 2);
        // console.log(vec.toString());
        // console.log(vec.normalize());
        const startVector2: Vector2 = new Vector2(100, 100);
        const endVector2: Vector2 = new Vector2(100, 150);
        // console.log(Vector2.getAngle(startVector2, endVector2));
        console.log(Vector2.getOrientation(startVector2, endVector2)); */
        const canvas: HTMLCanvasElement = document.getElementById('canvas') as HTMLCanvasElement;
        const app: TestApplication = new TestApplication(canvas);
        // app.start();
        app.render();
        // this.Vector2Test1(canvas, app);
        // this.Vector2Test2(canvas, app);
        // this.Vector2Test3(canvas, app);
        // this.Vector2Test4(canvas, app);
        this.Vector2Test5(canvas, app);
    }

    /**
     * *向量概念
     * @param canvas 
     * @param app 
     */
    public Vector2Test1(canvas: HTMLCanvasElement, app: TestApplication): void {
        let center: Vector2 = Vector2.create(canvas.width * 0.5, canvas.height * 0.5);
        let offset: Vector2 = Vector2.create(50, 50);
        if (app.context2D) {
            app.context2D.save();
            app.context2D.translate(center.x + offset.x, center.y + offset.y);
            app.context2D.rotate(Math2D.toRadian(45));
            app.drawVec(100, 10, "tail", "head", 3, false);
            app.context2D.restore();

            app.context2D.save();
            app.context2D.translate(center.x - offset.x, center.y + offset.y);
            app.context2D.rotate(Math2D.toRadian(135));
            app.drawVec(150, 10, "tail", "head", 3);
            app.context2D.restore();

            app.context2D.save();
            app.context2D.translate(center.x - offset.x, center.y - offset.y);
            app.context2D.rotate(Math2D.toRadian(-135))
            app.drawVec(200, 10, "tail", "head", 3);
            app.context2D.restore();

            app.context2D.save();
            app.context2D.translate(center.x + offset.x, center.y - offset.y);
            app.context2D.rotate(Math2D.toRadian(- 45))
            app.drawVec(250, 10, "tail", "head", 3);
            app.context2D.restore();
        }
    }

    /**
     * *向量的加减法示例
     * @param canvas 
     * @param app 
     */
    public Vector2Test2(canvas: HTMLCanvasElement, app: TestApplication): void {
        let isAdd: boolean = true;
        if (app.context2D) {
            let start: Vector2 = Vector2.create(200, 100);
            let end0: Vector2 = Vector2.create(400, 100);
            let end1: Vector2 = Vector2.create(400, 300);
            // 绘制同一个原点的两个向量
            app.drawVecFromLine(start, end0, 10, undefined, undefined, 1, false, true);
            app.drawVecFromLine(start, end1, 10, undefined, undefined, 1, false, true);
            if (isAdd) {
                // 加法
                // 首尾相连法加法，将start->end1向量与start-end0向量尾部相连
                app.drawVecFromLine(end0, Vector2.sum(end0, Vector2.difference(end1, start)), 10, undefined, undefined, 1, true, true);
                app.drawVecFromLine(end1, Vector2.sum(end1, Vector2.difference(end0, start)), 10, undefined, undefined, 1, true, true);
                let sum: Vector2 = Vector2.sum(end0, end1); // 
                sum = Vector2.difference(sum, start); // 相对start的偏移向量
                app.drawVecFromLine(start, sum, 10, undefined, undefined, 5, false, true, true);
            } else {
                // 减法
                app.drawVecFromLine(end0, end1, 10, undefined, undefined, 5, false, true, false);
            }
        }
    }
    /**
     * *负向量
     * @param canvas 
     * @param app 
     */
    public Vector2Test3(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {
            //在[ 500 , 400 ]绘制长度为200的向量
            app.context2D.save();
            app.context2D.translate(400, 300);
            app.context2D.rotate(Math2D.toRadian(45))
            app.drawVec(200);
            app.context2D.restore();

            //在[ 500 , 400 ]绘制长度为200的向量
            app.context2D.save();
            app.context2D.translate(400, 300);
            app.context2D.rotate(Math2D.toRadian(45))
            app.drawVec(- 200, 10, '', '', 3, true);
            app.context2D.restore();
        }
    }

    /**
     * *向量标乘示例
     * @param canvas 
     * @param app 
     */
    public Vector2Test4(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {
            let curr: number = 0;
            for (let i: number = 1; i < 14; i++) {
                app.context2D.save();
                app.context2D.translate(curr = i * 30, 300);
                app.context2D.rotate(Math2D.toRadian(100));
                app.drawVec(- 20 * i, 10);
                app.context2D.restore();
            }
            for (let i: number = 1; i < 14; i++) {
                app.context2D.save();
                app.context2D.translate(curr + i * 30, 300);
                app.context2D.rotate(Math2D.toRadian(100))
                app.drawVec(20 * i, 10);
                app.context2D.restore();
            }
        }
    }

    /**
     * *向量点乘
     * @param canvas 
     * @param app 
     */
    public Vector2Test5(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {
            app.context2D.save();
            app.context2D.translate(20, 300);
            app.context2D.save();
            app.context2D.rotate(Math2D.toRadian(30));
            app.drawVec(200, 10, undefined, "向量b", 3, false);
            app.context2D.restore();
            app.context2D.save();
            app.context2D.rotate(Math2D.toRadian(- 30));
            app.drawVec(160, 10, undefined, "向量a", 3, false);
            app.context2D.restore();
            app.context2D.restore();
            app.context2D.save();
            app.context2D.translate(300, 300);
            app.context2D.save();
            app.context2D.rotate(Math2D.toRadian(- 30));
            app.drawVec(160, 10, undefined, "向量a", 3, false);
            app.context2D.restore();
            app.context2D.save();
            app.context2D.rotate(Math2D.toRadian(60));
            app.drawVec(200, 10, undefined, "向量b", 3, false);
            app.context2D.restore();
            app.context2D.restore();
            app.context2D.save();
            app.context2D.translate(550, 300);
            app.context2D.save();
            app.context2D.rotate(Math2D.toRadian(- 60));
            app.drawVec(160, 10, undefined, "向量a", 3, false);
            app.context2D.restore();
            app.context2D.save();
            app.context2D.rotate(Math2D.toRadian(60));
            app.drawVec(200, 10, undefined, "向量b", 3, false);
            app.context2D.restore();
            app.context2D.restore();
        }
    }

    public Vector2Test6(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {

        }
    }

    public Vector2Test7(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {

        }
    }

    public Vector2Test8(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {

        }
    }

    public Vector2Test9(canvas: HTMLCanvasElement, app: TestApplication): void {
        if (app.context2D) {

        }
    }

}